/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.dwusoft.sendmsg.service.impl  
 */
package com.dwusoft.sendmsg.service.impl;

import java.util.List;

import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.exception.BusinessException;
import com.dwusoft.sendmsg.common.entity.Sendmsg;
import com.dwusoft.sendmsg.manager.SendmsgManager;
import com.apache.tools.DateUtils;

/**
 * description:  
 * @author Hou Dayu 创建时间：2016-5-18  
 * @Copyright(c)2014:北京东吴软件技术有限公司
 */
public class SendmsgManagerImpl implements SendmsgManager {

	protected IDao sendmsgDao;
	protected final String entityName = "com.dwusoft.sendmsg.common.entity.Sendmsg";

	public void setSendmsgDao(IDao sendmsgDao) {
		this.sendmsgDao = sendmsgDao;
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.dwusoft.api.manager.BaseManager#saveInfo(com.dwusoft.api.vo.ParamsVo)  
	 */
	public String saveInfo(ParamsVo<Sendmsg> vo) throws BusinessException {
		Sendmsg sendmsg = vo.getObj();
		String cacheKey = "";
		if (Validator.isNull(sendmsg.getMsgId())) {
			String infoId = Validator.generate();
			sendmsg.setMsgId(infoId);
		}
		sendmsg.setMsgCreateTime(DateUtils.Now.fmt_yyyyMMdd_HHmmss());
		sendmsg.setMsgPassNum("0");
		sendmsg.setMsgStatus("0");
		sendmsg.setMsgLevel(Validator.getDefaultStr(sendmsg.getMsgLevel(), "0"));
		sendmsg.setFromUserName(Validator.getDefaultStr(sendmsg.getFromUserName(), "系统"));
		MethodParam param = new MethodParam("Sendmsg", cacheKey, "", entityName);
		param.setVaule(sendmsg);
		if (sendmsgDao.insert(param)) {
			return sendmsg.getMsgId();
		}
		return "";
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.dwusoft.api.manager.BaseManager#editInfo(com.dwusoft.api.vo.ParamsVo)  
	 */
	public boolean editInfo(ParamsVo<Sendmsg> vo) throws BusinessException {
		Sendmsg sendmsg = vo.getObj();
		if (Validator.isNotNull(sendmsg.getMsgId())) {
			String cacheKey = "";
			MethodParam param = new MethodParam("Sendmsg", cacheKey, "", entityName);
			param.setVaule(sendmsg);
			return sendmsgDao.edit(param);
		}
		return false;
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.dwusoft.api.manager.BaseManager#deleteInfo(com.dwusoft.api.vo.ParamsVo)  
	 */
	public boolean deleteInfo(ParamsVo<Sendmsg> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		String cacheKey = "";
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		Sendmsg info = (Sendmsg) sendmsgDao.selectById(param);
		if (Validator.isEmpty(info)) {
			return false;
		}
		param.setVaule(info);//此处需要先将状态值赋值为删除状态
		boolean flag = false;
		if ("false".equals(mark)) {//逻辑删除
			param.setKey("Sendmsg");
			flag = sendmsgDao.edit(param);
		} else {
			param.setParams("msgId", infoId);
			param.setDelete(true);
			flag = sendmsgDao.delete(param);
		}
		return flag;
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.dwusoft.api.manager.BaseManager#getInfoById(com.dwusoft.api.vo.ParamsVo)  
	 */
	public Object getInfoById(ParamsVo<Sendmsg> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return sendmsgDao.selectById(param);
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.dwusoft.api.manager.BaseManager#execute(com.dwusoft.api.vo.ParamsVo)  
	 */
	public Object execute(ParamsVo<Sendmsg> vo) {

		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.dwusoft.api.manager.BaseManager#countInfo(com.dwusoft.api.vo.ParamsVo)  
	 */
	public long countInfo(ParamsVo<Sendmsg> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return sendmsgDao.count(param);
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.dwusoft.api.manager.BaseManager#getList(com.dwusoft.api.vo.ParamsVo)  
	 */
	public List<Sendmsg> getList(ParamsVo<Sendmsg> vo) {
		MethodParam param = setMethodParams(vo, 2);
		return sendmsgDao.select(param);
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.dwusoft.sendmsg.manager.SendmsgManager#getPageInfo(com.dwusoft.api.vo.ParamsVo)  
	 */
	public Page getPageInfo(ParamsVo<Sendmsg> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		Page page = sendmsgDao.pageSelect(param);
		return page;
	}

	private MethodParam setMethodParams(ParamsVo<Sendmsg> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		param.setResultCache(false);
		Sendmsg sendmsg = vo.getObj();
		if (Validator.isNotNull(sendmsg.getMsgType())) {
			param.setParams("msgType", sendmsg.getMsgType());
		}
		if (Validator.isNotNull(sendmsg.getMsgTitle())) {
			param.setParams("msgTitle", " '%" + sendmsg.getMsgTitle() + "%' ");
		}
		if (Validator.isNotNull(sendmsg.getFromUserName())) {
			param.setParams("fromUserName", sendmsg.getFromUserName());
		}
		if (Validator.isNotNull(sendmsg.getFromUserMobile())) {
			param.setParams("fromUserMobile", sendmsg.getFromUserMobile());
		}
		if (Validator.isNotNull(sendmsg.getFromUserEmail())) {
			param.setParams("fromUserEmail", sendmsg.getFromUserEmail());
		}
		if (Validator.isNotNull(sendmsg.getToUserName())) {
			param.setParams("toUserName", sendmsg.getToUserName());
		}
		if (Validator.isNotNull(sendmsg.getSpare1())) {
			param.setParams("spare1", sendmsg.getSpare1());
		}
		if (Validator.isNotNull(sendmsg.getSpare2())) {
			param.setParams("spare2", sendmsg.getSpare2());
		}
		if (Validator.isNotNull(sendmsg.getSpare3())) {
			param.setParams("spare3", sendmsg.getSpare3());
		}
		if (Validator.isNotNull(sendmsg.getToUserMobile())) {
			param.setParams("toUserMobile", sendmsg.getToUserMobile());
		}
		if (Validator.isNotNull(sendmsg.getToUserEmail())) {
			param.setParams("toUserEmail", sendmsg.getToUserEmail());
		}
		if (Validator.isNotNull(sendmsg.getMsgStatus())) {
			param.setParams("msgStatus", sendmsg.getMsgStatus());
		}

		return param;
	}
}
